/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package org.jackysoft.opensymphony.workflow.entity;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.jackysoft.util.XmlUtils;


/**
 * 
 */


public class ConditionDescriptor extends AbstractDescriptor implements Validatable {
    //~ Instance fields ////////////////////////////////////////////////////////

    /**
	 * 
	 */
	private static final long serialVersionUID = -1415374087504990728L;

	protected Map args = new HashMap();

    /**
     * The name field helps the editor identify the condition template used.
     */
    protected String name;
    protected String type;
    protected boolean negate = false;

    //~ Constructors ///////////////////////////////////////////////////////////

    /**
     * 
     */
    public ConditionDescriptor() { }

   
    public ConditionDescriptor(org.dom4j.Element element) {
    	super(element);
    	this.init(element);
    }

    //~ Methods ////////////////////////////////////////////////////////////////
    
	
	//关联表和外键属性
	
	//,inverseJoinColumns=
	
	//Map key元素的属性
	
	//Map 实体元素的属性
	
	//级联属性
	
    public Map getArgs() {
        return args;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setNegate(boolean negate) {
        this.negate = negate;
    }

    public boolean isNegate() {
        return negate;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void validate() throws InvalidWorkflowDescriptorException {
    }

    public void writeXML(PrintWriter out, int indent) {
        XmlUtils.printIndent(out, indent++);
        out.println("<condition " + (hasId() ? ("id=\"" + getId() + "\" ") : "") + (((name != null) && (name.length() > 0)) ? ("name=\"" + getName() + "\" ") : "") + (negate ? ("negate=\"true\" ") : "") + "type=\"" + type + "\">");

        Iterator iter = args.entrySet().iterator();

        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            XmlUtils.printIndent(out, indent);
            out.print("<arg name=\"");
            out.print(entry.getKey());
            out.print("\">");

            if ("beanshell".equals(type) || "bsf".equals(type)) {
                out.print("<![CDATA[");
                out.print(entry.getValue());
                out.print("]]>");
            } else {
                out.print(XmlUtils.encode(entry.getValue()));
            }

            out.println("</arg>");
        }

        XmlUtils.printIndent(out, --indent);
        out.println("</condition>");
    }
  
    protected void init(org.dom4j.Element condition) {
    	this.args = new HashMap();
    	type = condition.attributeValue("type");
    	String idstr = condition.attributeValue("id");
    	try {
    	    if(idstr!=null){
    	    	int id = Integer.parseInt(idstr);
    	    	this.setId(id);
    	    }
    	} catch (NumberFormatException e) {
    		logger.warn("the property of the condition element must be integer  ,but  "+idstr);
    	}
    	
    	String n = condition.attributeValue("negate");
    	
    	if ("true".equalsIgnoreCase(n) || "yes".equalsIgnoreCase(n)) {
    		negate = true;
    	} else {
    		negate = false;
    	}
    	
    	if (condition.attributeValue("name") != null) {
    		name = condition.attributeValue("name");
    	}
    	
    	for(Iterator argItr = condition.elementIterator("arg");argItr.hasNext();){
    		org.dom4j.Element arg = (org.dom4j.Element) argItr.next();
    		this.args.put(arg.attributeValue("name"), arg.getText());
    	}
    	
    }

	/**
	 * 
	 */
	public void setArgs(Map args) {
		this.args = args;
	}
}
